package com.wn.icloud.provider.uac.controller;

import com.wn.icloud.common.exception.BaseException;
import com.wn.icloud.common.msg.ObjectRestResponse;
import com.wn.icloud.common.vo.user.UserInfo;
import com.wn.icloud.provider.uac.remote.vo.LoginReqVO;
import com.wn.icloud.provider.uac.remote.vo.ResetUserPassWordReqVO;
import com.wn.icloud.provider.uac.service.MenuBiz;
import com.wn.icloud.provider.uac.service.UserBiz;
import com.wn.icloud.provider.uac.entity.Menu;
import com.wn.icloud.provider.uac.entity.User;
import com.wn.icloud.provider.uac.service.PermissionService;
import com.wn.icloud.provider.uac.service.dto.PasswordResetDTO;
import com.wn.icloud.provider.uac.vo.FrontUser;
import com.wn.icloud.provider.uac.vo.MenuTree;
import com.wn.icloud.provider.uac.vo.RegisterReqVO;
import com.wn.icloud.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Rainer
 * @create 2017-06-08 11:51
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<UserBiz, User> {
    @Autowired
    private PermissionService permissionService;

    @Autowired
    private MenuBiz menuBiz;

    @Autowired
    private UserBiz userBiz;


    /**
     * 获取登陆用户信息
     *
     * @return
     */
    @GetMapping(value = "/front/info")
    public ResponseEntity<FrontUser> getUserInfo() {
        FrontUser userInfo = permissionService.getUserInfo(getCurrentUserName());
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 获取用户菜单
     *
     * @return
     */
    @GetMapping(value = "/front/menus")
    public List<MenuTree> getMenusByUsername() {
        return permissionService.getMenusByUsername(getCurrentUserName());
    }

    /**
     * 获取所有菜单
     *
     * @return
     */
    @RequestMapping(value = "/front/menu/all", method = RequestMethod.GET)
    public @ResponseBody
    List<Menu> getAllMenus() {
        return menuBiz.selectListAll();
    }

    @PostMapping(value = "/resetPassword")
    public void resetPassword(@Valid @RequestBody PasswordResetDTO passwordResetDTO) {
        baseBiz.resetPassword(passwordResetDTO);
    }

    @PostMapping(value = "/updatePassword")
    public void updatePassword(@Valid @RequestBody PasswordResetDTO passwordResetDTO) {
        passwordResetDTO.setUserId(getCurrentUserId());
        baseBiz.updatePassword(passwordResetDTO);
    }

    /**
     * 注册-手机号码方式
     */
    @PostMapping(value = "/register")
    public ObjectRestResponse register(@RequestBody RegisterReqVO registerReqVO) throws BaseException {
        checkNotNull(registerReqVO);
        checkNotNull(registerReqVO.getMsgCode());
        checkNotNull(registerReqVO.getPassword());
        checkNotNull(registerReqVO.getPhoneNum());
        return userBiz.register(registerReqVO);
    }

    /**
     * 获取个人信息
     */
    @GetMapping(value = "/getUserInfo")
    public ObjectRestResponse getUserInfoEnterprise() {
        String userid = getCurrentUserId();
        return userBiz.getUserInfoEnterprise(userid);
    }

    /**
     * 修改密码
     */
    @PostMapping(value = "/updateUserPassword")
    public ObjectRestResponse updateUserPassword(@RequestBody ResetUserPassWordReqVO ResetUserPassWordReqVO) throws BaseException {
        String userid = getCurrentUserId();
        return userBiz.updateUserPassword(ResetUserPassWordReqVO,userid);
    }

}
